package com.jxwifi.cloud.quickcleanserver.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.bean.PoiItemBean;
import com.jxwifi.cloud.quickcleanserver.workmap.MapSearchActivity;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/5/22.
 */

public class mMapSearchAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<PoiItemBean> mList = new ArrayList<>();

    public mMapSearchAdapter(Context ctx) {
        mContext = ctx;
    }

    /**
     * 下拉刷新时调用
     *
     * @param list
     */
    public void refresh(List<PoiItemBean> list) {
        mList.clear();
        addData(list);
    }

    public void updata() {
        mList.clear();
        notifyDataSetChanged();
    }

    //添加数据
    public void addData(List<PoiItemBean> list) {
        if (list.size() != 0 || list != null) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_map_search, null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder mViewHolder = (ViewHolder) holder;
        final PoiItemBean mAskBean = mList.get(position);
        if (position==0){
            mViewHolder.mImgMapSearch.setVisibility(View.VISIBLE);
        }else{
            mViewHolder.mImgMapSearch.setVisibility(View.GONE);
        }
        mViewHolder.mTvMapSearchAddress.setText(mAskBean.getTitle());
        mViewHolder.mTvMapSearchAddressDetailed.setText(mAskBean.getSnippet());
        mViewHolder.mRelMapSearchLayout.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                EventBus.getDefault().post(mAskBean, "e-searchlocation");
                MapSearchActivity activity = (MapSearchActivity)mContext;
                activity.finish();
            }
        });



    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View mContainer;
        RelativeLayout mRelMapSearchLayout;
        ImageView mImgMapSearch;//图标
        TextView mTvMapSearchAddress;//地址
        TextView mTvMapSearchAddressDetailed;//详细地址


        public ViewHolder(View itemView) {
            super(itemView);
            mContainer = itemView;
            mRelMapSearchLayout=(RelativeLayout)itemView.findViewById(R.id.rel_map_search_layout);
            mImgMapSearch=(ImageView)itemView.findViewById(R.id.img_map_search);
            mTvMapSearchAddress = (TextView) itemView.findViewById(R.id.tv_map_search_address);
            mTvMapSearchAddressDetailed = (TextView) itemView.findViewById(R.id.tv_map_search_address_detailed);
        }
    }
}
